"""
下载  命令
pip install requests_html -i https://pypi.tuna.tsinghua.edu.cn/simple

pip install lxml_html_clean -i https://pypi.tuna.tsinghua.edu.cn/simple

pip install fake_useragent==1.5.1 -i https://pypi.tuna.tsinghua.edu.cn/simple
"""

from requests_html import HTMLSession
from lxml import etree

requests = HTMLSession()
for i in range(1, 5):
    print(f"------------------正在爬取第{i}页------------------")
    url = f"https://cs.lianjia.com/ershoufang/pg{i}/"

    response = requests.get(url)

    tree = etree.HTML(response.text)

    li_list = tree.xpath('//ul[@class="sellListContent"]/li')

    for li in li_list:
        标题 = li.xpath('.//div[@class="title"]/a/text()')
        位置 = li.xpath('.//div[@class="flood"]//a/text()')
        户型 = li.xpath('.//div[@class="address"]/div[@class="houseInfo"]/text()')
        总价 = li.xpath('.//div[@class="priceInfo"]/div[@class="totalPrice totalPrice2"]/span/text()')
        单价 = li.xpath('.//div[@class="priceInfo"]/div[@class="unitPrice"]/span/text()')

        print(标题)
        print(位置)
        print(户型)
        print(总价)
        print(单价)

        print("-----------------------")

